package org.example;

import org.example.pojo.Product;
import org.example.pojo.PurchaseInfo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) throws IOException {
        // 要读取的文件
        String fileName = "D:\\JavaProject\\demo01\\demo.txt";
        DeepSelect(EasySelect(fileName));
    }

    // 简单筛选
    public static List<String> EasySelect(String fileName) throws IOException {

        String[] strings = SplitFile(LoadFromLocal(fileName), "\\【"); // \n\r\n 匹配空行 改为 匹配\【 符号

        // 将 数组 转为 ArrayList
        ArrayList<String> list = new ArrayList<>(strings.length);
        Collections.addAll(list, strings);
        System.out.println("START SIZE: " + list.size());
        int i = 0;
        for (Iterator<String> it = list.iterator(); it.hasNext(); i++) {
//            System.out.println(RemoveBlankLine(it.next().toString()));
//            System.out.println("-----------------------");
            String str = RemoveBlankLine(it.next());
            PurchaseInfo purchaseInfo = new PurchaseInfo();
            // 筛选用户信息
            boolean b = SelectBaseInfo(purchaseInfo, str);
            if (b) {
                // 筛选产品信息, 添加到产品信息父类中
                purchaseInfo.setProducts(SelectProductInformation(str));
//                System.out.println(purchaseInfo.toString());
//                System.out.println("------------------------");
                // 存在则删除指定元素
                it.remove();
            }else {
                System.out.println("FAILED: "+ str);
            }
        }

        System.out.println("END SIZE: " + list.size());
        return list;
    }

    // 深度筛选
    public static String DeepSelect(List<String> list) {
        return null;
    }

    // 读取
    public static String LoadFromLocal(String fileName) throws IOException {
        return Files.readString(Paths.get(fileName));
    }

    // 字符串分割
    public static String[] SplitFile(String baseString, String regex) {
        return baseString.split(regex);
        // \r\n
    }

    // 字符串格式化
    public static String FormatString(String baseString) {
        return baseString.replaceFirst("\r?\n", " ");
    }


    // 筛选基础数据 (职位, 用户名, QQ 账号, 发布时间)
    public static boolean SelectBaseInfo(PurchaseInfo purchaseInfo, String str) {
//        String regex = "\\【(.*?)\\】(.*?)\\((.*?)\\)\\s(\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})";
        String regex = "(.*?)\\】(.*?)[<,(](.*?)[),>]\\s(\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(str);

        if (matcher.find()) {
            // 职位
            String position = matcher.group(1);
            String username = matcher.group(2);
            String QQ_code = matcher.group(3);
            String time = matcher.group(4);

            purchaseInfo.setPosition(position);
            purchaseInfo.setName(username);
            purchaseInfo.setQQ_Code(QQ_code);
            purchaseInfo.setReleaseTime(time);

            // 识别出来的返回 true
            return true;
        } else {
            return false;
        }
    }


    // 筛选产品数据 (型号数量)
    public static List<Product> SelectProductInformation(String str) {


        return null;

    }

    // 删除空白行
    public static String RemoveBlankLine(String src) {
        return src.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1").replaceAll("^((\r\n)|\n)", "");

    }


}


